<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>待办任务</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="handle">办理</a>
        </script>
    </div>
</div>
<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../js/lay-config.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        var insTb = table.render({
            elem: '#currentTableId',
            url: publicUrl + 'task/getTasks',
            toolbar: '#toolbarDemo',
            defaultToolbar: [],
            cols: [[
                {type: "checkbox", width: '5%', align: "center"},
                {field: 'id', width: '15%', title: '任务id', align: "center"},
                {field: 'instanceName', width: '10%', title: '流程名称', align: "center"},
                {field: 'name', width: '10%', title: '任务名称', align: "center"},
                {field: 'status', width: '15%', title: '任务状态', align: "center"},
                {field: 'assignee', width: '10%', title: '办理人', align: "center"},
                {field: 'createDate', width: '15%', title: '创建时间', align: "center"},
                {title: '操作', width: '20%', toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line',
            parseData: function (res) {
                // 处理时间和状态
                for(var i = 0 ; i< res.data.items.length ; i++) {
                    res.data.items[i]["createDate"] = renderTime(res.data.items[i]["createDate"]);
                    res.data.items[i]["status"] = judgeState(res.data.items[i]["status"]);
                }

                return {
                    "code": 0,
                    "msg": res.msg,
                    "count": res.data.count,
                    "data": res.data.items
                };
            },
            done: function (res, curr, count) {
                if (res.code == 500) {
                    layer.alert(res.msg, {icon: 2, title: "提示"});
                }
            }
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);

            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                },
                where: {
                    params: result
                }
            }, 'data');

            return false;
        });

        // 监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        // tool监听事件
        table.on('tool(currentTableFilter)', function (obj) {
            // 办理
            if (obj.event === 'handle') {
                renderForm(obj);
            }
        });

        /**
         * 处理状态
         */
        function judgeState(state) {
            if (state == 'ASSIGNED') {
                state = '执行中';
            }
            return state
        }

        /**
         * 动态生成表单
         */
        function renderForm(obj) {
            var loadIndex = layer.load(2);
            $.post(publicUrl + 'task/renderForm', {
                taskId: obj.data ? obj.data.id : '',
            }, function (res) {
                layer.close(loadIndex);
                if (res.code === 200) {
                    if (res.data === '无表单') {
                        completeTask(obj);
                    } else {
                        var index = layer.open({
                            title: '办理任务',
                            type: 2,
                            shade: 0.2,
                            maxmin: true,
                            shadeClose: true,
                            area: ['100%', '100%'],
                            content: '../page/task-form.html',
                            success: function (layero, index) {
                                // 向跳转页面传递参数
                                var iframe = window['layui-layer-iframe' + index];
                                iframe.child(obj.data.id, JSON.stringify(res.data));
                            },
                            end: function () {
                                insTb.reload();
                            }
                        });
                        $(window).on("resize", function () {
                            layer.full(index);
                        });
                    }
                } else {
                    parent.layer.msg(res.msg, {icon: 0, time: 2000});
                }
            }, 'json');
        }

        /**
         * 完成任务
         * @param obj
         */
        function completeTask(obj) {
            layer.confirm('确定要办理该任务吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (index) {
                layer.close(index);
                var loadIndex = layer.load(2);
                $.post(publicUrl + 'task/completeTask', {
                    taskId: obj.data ? obj.data.id : '',
                }, function (res) {
                    layer.close(loadIndex);
                    if (res.code === 200) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload();
                    } else {
                        layer.alert(res.msg, {icon: 2, title: '提示'});
                    }
                }, 'json');
            });
        }
    });
</script>

</body>
</html>